﻿namespace com.stifel.BranchMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.stifel.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = BranchMgmtDataContext;

    using CustomObject = StifelBranchInfo;

    public class StifelBranchInfoDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelbranchinfo";

        #endregion Fields

        #region Constructors

        public StifelBranchInfoDao( )
            : base(ListName)
        {
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelBranchInfoDao( string currentWebUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public CustomObject Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                string title = value.Title;
                List<SPFile> attachments = value.Attachments;
                Guid g = Guid.NewGuid( );
                value.Title = g.ToString( );
                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
                CustomObject v = ( from o in l
                                   where o.Title == g.ToString( )
                                   select o ).SingleOrDefault( );
                v.Title = title;
                c.SubmitChanges( );

                if ( attachments != null && attachments.Count > 0 )
                    v.ListItem.AddAttachmentNow( attachments );

                return v;
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void Delete( Guid uniqueId )
        {
            //TODO
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == uniqueId
                                   select o ).SingleOrDefault( );
                if ( v != null )
                {
                    l.DeleteOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public CustomObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.Id == id
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject Get( string branchId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.BranchLocationIdentifier.Equals( branchId, StringComparison.OrdinalIgnoreCase )
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject Get( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == uniqueId
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<CustomObject> l = c.GetList<CustomObject>( ListTitle ).ToList( );
                //if ( l.Count > 0 )
                //    l.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
                return ( sortBySiteId( l ) );
            }
        }

        public List<CustomObject> GetByBroadcastIP( string code )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                List<CustomObject> v = ( from o in l
                                         where o.BroadcastIP.Contains( code )
                                         select o ).ToList( );
                //if ( v.Count > 0 )
                //    v.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
                return ( sortBySiteId( v ) );
            }
        }

        public List<CustomObject> GetByCheckCode( string code )
        {
            StifelBranchUnitDao dao = new StifelBranchUnitDao( CurrentWebUrl );
            List<StifelBranchUnit> u = dao.GetByCheckCode( code );
            List<CustomObject> l = new List<CustomObject>( );
            foreach ( StifelBranchUnit v in u )
            {
                if ( v.BranchLookupId.HasValue )
                {
                    CustomObject o = Get( v.BranchLookupId.Value );
                    if ( o != null )
                        l.Add( o );
                }
            }
            //if ( l.Count > 0 )
            //    l.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
            return ( sortBySiteId( l ) );
        }

        public List<CustomObject> GetByDestinationCode( string code )
        {
            StifelBranchUnitDao dao = new StifelBranchUnitDao( CurrentWebUrl );
            List<StifelBranchUnit> u = dao.GetByDestinationCode( code );
            List<CustomObject> l = new List<CustomObject>( );
            foreach ( StifelBranchUnit v in u )
            {
                if ( v.BranchLookupId.HasValue )
                {
                    CustomObject o = Get( v.BranchLookupId.Value );
                    if ( o != null )
                        l.Add( o );
                }
            }
            //if ( l.Count > 0 )
            //    l.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
            return ( sortBySiteId( l ) );
        }

        public List<CustomObject> GetByORG( string code )
        {
            StifelBranchUnitDao dao = new StifelBranchUnitDao( CurrentWebUrl );
            List<StifelBranchUnit> u = dao.GetByORG( code );
            List<CustomObject> l = new List<CustomObject>( );
            foreach ( StifelBranchUnit v in u )
            {
                if ( v.BranchLookupId.HasValue )
                {
                    CustomObject o = Get( v.BranchLookupId.Value );
                    if ( o != null )
                        l.Add( o );
                }
            }
            //if ( l.Count > 0 )
            //    l.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
            return ( sortBySiteId( l ) );
        }

        public List<CustomObject> GetByPriceServer( string code )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                List<CustomObject> v = ( from o in l
                                         where o.PriceServer.Equals( code, StringComparison.OrdinalIgnoreCase )
                                         select o ).ToList( );
                //if ( v.Count > 0 )
                //    v.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
                return ( sortBySiteId( v ) );
            }
        }

        public CustomObject GetBySiteId( string code )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.SiteId.Equals( code, StringComparison.OrdinalIgnoreCase )
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject GetBySpeedDial( string code )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.SpeedDial == code
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject GetByThompsonIdPrefix( string code )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.ThompsonIdPrefix.Equals( code, StringComparison.OrdinalIgnoreCase )
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<CustomObject> GetByUnit( string code )
        {
            StifelBranchUnitDao dao = new StifelBranchUnitDao( CurrentWebUrl );
            List<StifelBranchUnit> u = dao.GetByUnit( code );
            List<CustomObject> l = new List<CustomObject>( );
            foreach ( StifelBranchUnit v in u )
            {
                if ( v.BranchLookupId.HasValue )
                {
                    CustomObject o = Get( v.BranchLookupId.Value );
                    if ( o != null )
                        l.Add( o );
                }
            }
            //if ( l.Count > 0 )
            //    l.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
            return ( sortBySiteId( l ) );
        }

        public List<CustomObject> GetByWireCode( string code )
        {
            StifelBranchUnitDao dao = new StifelBranchUnitDao( CurrentWebUrl );
            List<StifelBranchUnit> u = dao.GetByWireCode( code );
            List<CustomObject> l = new List<CustomObject>( );
            foreach ( StifelBranchUnit v in u )
            {
                if ( v.BranchLookupId.HasValue )
                {
                    CustomObject o = Get( v.BranchLookupId.Value );
                    if ( o != null )
                        l.Add( o );
                }
            }
            //if ( l.Count > 0 )
            //    l.Sort( ( a, b ) => a.SiteId.ToUpper( ).CompareTo( b.SiteId.ToUpper( ) ) );
            return ( sortBySiteId( l ) );
        }

        CustomObject clean( CustomObject value )
        {
            return new CustomObject
            {
                BranchCity = value.BranchCity,
                BranchLocationIdentifier = value.BranchLocationIdentifier,
                BranchStreetAddress = value.BranchStreetAddress,
                BranchSuiteAddress = value.BranchSuiteAddress,
                BranchTypeId = value.BranchTypeId,
                BranchZipcode = value.BranchZipcode,
                BroadcastIP = value.BroadcastIP,
                FreightElevator = value.FreightElevator,
                PriceServer = value.PriceServer,
                ScheduledOrActualConnectivityDate = value.ScheduledOrActualConnectivityDate,
                ScheduledOrActualOpenDate = value.ScheduledOrActualOpenDate,
                ScheduledOrActualRelocationDate = value.ScheduledOrActualRelocationDate,
                ShipDock = value.ShipDock,
                SiteId = value.SiteId,
                SpeedDial = value.SpeedDial,
                ThompsonIdPrefix = value.ThompsonIdPrefix,
                TimeZoneLookupId = value.TimeZoneLookupId,
                Title = value.Title,
                UnionShop = value.UnionShop,
                USStatesLookupId = value.USStatesLookupId
            };
        }

        List<CustomObject> sortBySiteId( List<CustomObject> list )
        {
            if ( list.Count > 0 )
                list.Sort( ( a, b ) =>
                {
                    string x = string.IsNullOrEmpty( a.SiteId ) ? string.Empty : a.SiteId.ToUpper( );
                    string y = string.IsNullOrEmpty( b.SiteId ) ? string.Empty : b.SiteId.ToUpper( );
                    return x.CompareTo( y );
                } );
            return list;
        }

        #endregion Methods
    }
}